﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace ClientRegister
{
    public partial class CourseAndClassOfStudentForm : Form
    {
        DataSet ds;

        SynchronizeData sync = new SynchronizeData();

        public string IDEmp { set; get; }
        public string NameEmp { set; get; }

        public CourseAndClassOfStudentForm()
        {
            InitializeComponent();
        }

        private void CourseAndClassOfStudentForm_Load(object sender, EventArgs e)
        {

        }
        //Nhan ID cua hoc vien va Tim khoa hoc cua hoc vien va do vao combobox
        private void txtID_TextChanged(object sender, EventArgs e)
        {
            CourseDataProvider course = new CourseDataProvider();
            if (txtID.Text != "")
            {
                DataSet ds = course.loadCoursesOfStudent(txtID.Text.ToString());
                DataTable dt = ds.Tables[0];
                cbbxCourse.DataSource = dt;
                cbbxCourse.DisplayMember = "NameCourse";
                cbbxCourse.ValueMember = "ID";
            }
            else
            {
                cbbxCourse.DataSource = null;
            }
        }
        //Chon khoa hoc se dua ra danh Lop hoc tuong ung voi khoa hoc do
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbbxCourse.SelectedValue.ToString() == "System.Data.DataRowView") return;
            else
            {
                ClassDataProvider cls = new ClassDataProvider();
                ds = cls.loadClassOfStudent(txtID.Text.ToString(),cbbxCourse.SelectedValue.ToString());
                GridListClass.DataSource = ds.Tables[0];
            }
        }

        private void btnEditClass_Click(object sender, EventArgs e)
        {
            if(GridListClass.SelectedCells.Count > 0)
            {
                ChangeClassForStuForm editC = new ChangeClassForStuForm();
                editC.IDEmp = IDEmp;
                editC.NameEmp = NameEmp;
                editC.IDStu = txtID.Text.ToString();
                editC.IDClass = GridListClass.CurrentRow.Cells["ID"].Value.ToString();
                editC.IDSub = GridListClass.CurrentRow.Cells["IDSubject"].Value.ToString();
                editC.Show();
            }
        }

        private void btnEditCourse_Click(object sender, EventArgs e)
        {
            DataTable dtClass = ds.Tables[0];
            string[] idSub = new string[dtClass.Rows.Count];
            string[] idClass = new string[dtClass.Rows.Count];

            ClassDataProvider cls = new ClassDataProvider();

            for (int i = 0; i < dtClass.Rows.Count; i++)
            {
                idClass[i] = dtClass.Rows[i].ItemArray[0].ToString();
                SqlDataReader sdr = cls.SubjectInClass(idClass[i].ToString());
                if(sdr.Read())
                    idSub[i] = sdr.GetValue(0).ToString();
                sdr.Close();
            }

            ChangeCourseForStuForm changeCour = new ChangeCourseForStuForm();
            changeCour.IdClass = idClass;
            changeCour.IDStu = txtID.Text.ToString();
            changeCour.IdSub = idSub;
            changeCour.IdCourse = cbbxCourse.SelectedValue.ToString();
            changeCour.IDEmp = IDEmp;
            changeCour.NameEmp = NameEmp;
            changeCour.Show();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
